import Taro from '@tarojs/taro';

import {Command} from '../constant';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';

import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    //市
    async findMeituanCities(index, provinceId) {
      let {filterData, filterDropdownValue} = getData().main;
      const res = await api.MeituanController.findMeituanCities({provinceId});
      let arr = Object.keys(res.context).map((key) => ({
        code: key,
        name: res.context[key],
        value: res.context[key],
      }));
      filterData[0].children[index].children = arr;
      let key, code;
      arr.map((v, k) => {
        if (v.name == '南京') {
          key = k;
          code = v.code;
        }
      });
      action.commonChange([{paths: 'main.filterData', value: filterData}]);
      if (code) {
        action.findMeituanOneRegions(code, 1); //默认南京
        let oneArr = [index, key]; //地址初始值
        filterDropdownValue[0] = oneArr;
        filterDropdownValue[1] = [[0, 0]];
        action.commonChange([
          {
            paths: 'main.filterDropdownValue',
            value: filterDropdownValue,
          },
        ]);
      } else {
        action.findMeituanOneRegions(arr[0].code, 2); //其他请求第一个
      }
    },
    //商圈
    async findMeituanOneRegions(cityId, type) {
      let {filterData, filterDropdownValue} = getData().main;
      const res = await api.MeituanController.findMeituanOneRegions({cityId});
      let leftArr = Object.keys(res.context.districtInfos).map((key) => ({
        code: key,
        name: res.context.districtInfos[key],
        value: res.context.districtInfos[key],
      }));
      leftArr.map((item, index) => {
        if (res.context.districtToRegionMaps[item.code]) {
          item.children = Object.keys(res.context.districtToRegionMaps[item.code]).map((key) => ({
            code: key,
            name: res.context.districtToRegionMaps[item.code][key],
            value: res.context.districtToRegionMaps[item.code][key],
          }));
        }
      });
      filterData[1].children = leftArr;
      action.commonChange('main.filterData', filterData);
      filterDropdownValue[1] = [0, 0]; //商圈初始值
      action.commonChange([
        {
          paths: 'main.filterDropdownValue',
          value: filterDropdownValue,
        },
        {
          paths: 'main.cityId',
          value: cityId,
        },
        {
          paths: 'main.regionId',
          value: leftArr[0].children[0].code,
        },
      ]);
      //商品数据
      action.page();
    },

    async page() {
      let {page, size, List, cityId, cat0Id, cat1Id, regionId, sortType, keyWords} = getData().main;
      const params = {
        cat0Id, //一级类目
        cat1Id, //二级类目
        // dealType: 1, //团单类型:1.套餐,2代金券
        geo: {
          cityId, //城市信息
          // districtId: 320000, //行政区信息 320000江苏
          lng: Taro.getStorageSync('longitude'), //经度,
          lat: Taro.getStorageSync('latitude'), //纬度
          // radii: 10000, //半径 单位:米
          regionId, //商圈信息 山西路/湖南路
        },
        keyWords, //搜索关键词
        page, //页数,从1开始
        size, //每页结果数
        sortType, //1距离离我最近15门店评分降序19门店人均价格降序
      };
      const res = await api.MeituanController.findMeituanSearchDeals(params);
      if (res.code == 'K-000000') {
        this.commonChange('main.totalPage', res.context.recordCount);
        if (page == 1) {
          action.commonChange('main.List', res.context.records);
        } else {
          let newList = List.concat(res.context.records);
          action.commonChange('main.List', newList);
        }
      } else {
        Taro.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000,
        });
      }
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageDmeituan'),
  };
}
